import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.htmlparser.filters.IsEqualFilter;
import org.omg.CORBA.RepositoryIdHelper;

import database.HibernateUtil;
import database.model.Article;
import database.model.Section;


public class ArticleServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONArray ja = new JSONArray();
		response.setContentType("application/json");       
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
	    PrintWriter out = response.getWriter();                               	    
	    Session se = HibernateUtil.currentSession();
  		Transaction trans = se.beginTransaction();
  		List<Section> list =  se.createCriteria(Section.class).setFetchSize(1).add(Restrictions.like("url", "http://vnexpress.net/rss/gl/the-thao.rss")).list();
  		Section section = list.get(0);
  		List<Article> articles = section.getArticles();
  		for(Article anArticle:articles)
  		{
  			System.out.println(anArticle.getUrl());
  		}
  		Article a = section.getArticles().get(1);
  		String docType =
  		      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
  		    out.println(docType +
  		                "<html>\n" +
  		                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
  		                "<head><title>" +"Thâu thập" + "</title></head>\n" +
  		                "<body>\n" +
  		                "<h1 align=\"center\">" +"Thâu thập" + "</h1>\n" +
  		                "<ul>\n" +
  		                "  <li>Parameter 1: "
  		                +
  		                "  <li>Parameter 2: "
  		                +a.getContent() + "\n" +
  		                "</ul>\n" +
  		                "</body></html>");
  		out.print(docType);
  		
  		trans.commit();
  		HibernateUtil.closeSession();
	    out.close();
	    
	    response.setHeader("", "");
		  
	}

	/*int count;

	  public void init() throws ServletException {
	    FileReader fileReader = null;
	    BufferedReader bufferedReader = null;
	    try {
	      fileReader = new FileReader("InitDestroyCounter.initial");
	      bufferedReader = new BufferedReader(fileReader);
	      String initial = bufferedReader.readLine();
	      count = Integer.parseInt(initial);
	      bufferedReader.close(); 
	      return;
	    }
	    catch (Exception ignored) { }

	    String initial = getInitParameter("initial");                    
	    try {                                                            
	      count = Integer.parseInt(initial);                             
	      return;                                                        
	    }                                                                
	    catch (NumberFormatException ignored) { }  // null or non-integer value
	    count = 0;                                                       
	  }                                                                  
	                                                                     
	  public void doGet(HttpServletRequest request, HttpServletResponse response) 
	                               throws ServletException, IOException {
	    res.setContentType("text/html");       
	    res.setCharacterEncoding("UTF-8");
	    PrintWriter out = res.getWriter();                               
	    count++;                                                         
	    out.println(ServletContextApplication.sharedContext()
	    		.getArticleSyncManager()
	    		.getArticle()
	    		.getContentDetail());  
	    out.close();
		  request.setCharacterEncoding("UTF-8");
		    response.setCharacterEncoding("UTF-8");
		    response.setContentType("text/html; charset=UTF-8");
		    PrintWriter out = response.getWriter();
		    String title = "Request Parameters";
		    String docType =
		      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
		    out.println(docType +
		                "<html>\n" +
		                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
		                "<head><title>" +"Thâu thập"+ title + "</title></head>\n" +
		                "<body>\n" +
		                "<h1 align=\"center\">" +"Thâu thập"+ title + "</h1>\n" +
		                "<ul>\n" +
		                "  <li>Parameter 1: "
		                + ConvertService.toUTF8(request.getParameter("param1")) + "\n" +
		                "  <li>Parameter 2: "
		                +( ServletContextApplication.sharedContext().getArticleSyncManager()
			    		.getArticle()
			    		.getContentDetail()) + "\n" +
		                "</ul>\n" +
		                "</body></html>");
	  }                                                                  
	                                                                     
	  public void destroy() {                                            
	    super.destroy();
	    saveState();                                                     
	  }                                                                  
	                                                                     
	  public void saveState() {                                          
	    FileWriter fileWriter = null;
	    PrintWriter printWriter = null;
	    try {                                                            
	      fileWriter = new FileWriter("InitDestroyCounter.initial");
	      printWriter = new PrintWriter(fileWriter);         
	      printWriter.println(count);                                  
	      printWriter.close();
	      return;                                                        
	    }                                                                
	    catch (IOException e) {
	    }
	  }*/
}
